//工具函数层（底层） 设置服务器地址/设置超时时间/请求拦截器/响应拦截器
//导入axios
import axios from "axios";

const service = axios.create({
  // 192.168.0.74:8091
  baseURL: "http://192.168.8.76:8091", // 凉山州ip
  // baseURL: "http://192.168.0.72:8091",
  timeout: 900000,
});

//响应拦截
service.interceptors.response.use(
  (res) => { 
    if (res.data?.code == "401") {
      let cancelArr = window.axiosCancel;
      cancelArr.forEach((ele, index) => {
        ele("取消请求");
        delete window.axiosCancel[index];
      });
      // let res = window.confirm("登录失效，请重新登录")
    }
    // 响应成功返回数据
    return res.data;
  },
  (error) => {
    return Promise.reject(error);
  }
);

export default function axiosReq({
  method,
  url,
  data,
  params,
  befLoading,
  aftLoading,
}) {
  return service({
    method: method || "get",
    url,
    data: data || {},
    params: params || "",
    befLoading: befLoading || false,
    aftLoading: aftLoading || true,
  });
}
